package com.fingard.dsp.bank.directbank.cgbunion01;

import com.alibaba.fastjson.JSONObject;
import com.fingard.constant.Format;
import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IQRPay;
import com.fingard.dsp.bank.reqrespobj.qrpay.ReqGenQRInfo;
import com.fingard.dsp.bank.reqrespobj.qrpay.RetAtsGenQRInfo;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.ReqUnifiedOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsQueryOrder;
import com.fingard.dsp.bank.reqrespobj.unifiedorder.RetAtsUnifiedOrder;
import com.fingard.text.StringHelper;

import java.util.HashMap;
import java.util.Map;

public class CGBUnion01QRPay extends CGB01UnifiedOrder implements IQRPay {
    @Override
    public RetAtsUnifiedOrder submitQROrder(ReqUnifiedOrder p_req) {
        RetAtsUnifiedOrder retAts = new RetAtsUnifiedOrder();
        retAts.rdSeq = p_req.rdSeq;
        try{
            ActSetItem actItem = this.getBankConfig().getActSetItem(p_req.ownAct, "");
            if (actItem != null) {
                Map<String, String> headMap = buildHeaderParam("preOrderJH",actItem);
                Map<String, Object> bodyMap = new HashMap<String, Object>();
                bodyMap.put("service", "unified.trade.native");
                bodyMap.put("mch_id", actItem.merchantId);//商户appId
                bodyMap.put("out_trade_no", p_req.rdSeq);
                bodyMap.put("body", "123");
                bodyMap.put("total_fee", StringHelper.amountMultiply100(p_req.amount));//商户appId
                bodyMap.put("attach", p_req.attach);
                String ip = actItem.createIP;
                if (StringHelper.hasAnyChar(ip)) {
                    bodyMap.put("mch_create_ip", ip);//后台通知地
                }
                String notify_url = actItem.notifyURL;
                if (StringHelper.hasAnyChar(notify_url)) {
                    bodyMap.put("notify_url", notify_url);//后台通知地
                }
                boolean limit = getBankConfig().getValueAsBool("limitPay");
                WriteBankLogLn("广发聚合能否用信用卡支付："+limit);
                if(!limit){
                    bodyMap.put("limit_credit_pay", "1");
                }
                if (StringHelper.hasAnyChar(p_req.timeStart)) {
                    bodyMap.put("time_start", Format.DateTime14Format.format(Format.DateTime14Format.parse(p_req.timeStart)));
                }
                if (StringHelper.hasAnyChar(p_req.timeExpire)) {
                    bodyMap.put("time_expire", Format.DateTime14Format.format(Format.DateTime14Format.parse(p_req.timeExpire)));
                }
                bodyMap.put("nonce_str", get_nonce_str());
                bodyMap.put("sign", "asdf");
                if ("NATIVE01".equalsIgnoreCase(p_req.reqHeader.commandCode)) {
                    //微信
                    bodyMap.put("payWay", "01_WXPAY_STCTP");
                }else if("NATIVE02".equalsIgnoreCase(p_req.reqHeader.commandCode)){
                    //支付宝
                    bodyMap.put("payWay", "01_ALIPAY_STCTP");
                }else{
                    retAts.respCode = "-1";
                    retAts.respInfo = "未知的线路指令代码";
                    return retAts;
                }
                JSONObject preOrder  = new JSONObject();
                preOrder.put("Header",headMap);
                preOrder.put("Body",bodyMap);
                String returnStr = send(preOrder,"preOrderJH",actItem);
                JSONObject resultObj = JSONObject.parseObject(returnStr);
                JSONObject resBody = resultObj.getJSONObject("Body");
                String err_code = resBody.getString("errorCode");
                String err_msg = resBody.getString("errorMsg");
                if(StringHelper.hasAnyChar(err_code)){
                    retAts.respCode = "-1";
                    retAts.respInfo = err_msg;
                }else {
                    String result_code = resBody.getString("result_code");
                    retAts.respCode = "0";
                    if("0".equals(result_code)){
                        retAts.respInfo = "请求成功";
                        retAts.qrInfo = resBody.getString("code_url");
                    }
                }

            } else {
                retAts.respCode = "-1";
                retAts.respInfo = "未找到账号[" + p_req.ownAct + "]商户配置信息!";
                retAts.qrInfo = "";
            }
        }catch (Exception ex) {
            ex.printStackTrace();
            retAts.respCode = "-1";
            retAts.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }
        return retAts;
    }

    @Override
    public RetAtsQueryOrder queryQRPay(ReqQueryOrder p_req) {
        return queryUnifiedOrder(p_req);
    }
    @Override
    public RetAtsGenQRInfo genQRInfo(ReqGenQRInfo p_req) {
        return null;
    }
}
